package com.contactbook.web.client;


import com.contactbook.web.client.dto.AccountDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LoginWidget extends View {

	private VerticalPanel content;
	
	private final static String TITLE = "Connect to your contact book.";
	
	private final TextBox textBoxLogin;
	private final PasswordTextBox textBoxPassword;
	
	private final static String LOGIN_TEXTBOX_SIZE = "150";
	private final static String PASSWORD_TEXTBOX_SIZE = "150";
	
	public LoginWidget(ContactBook contactBook) {
		
		super(contactBook);
		textBoxLogin = new TextBox();
		textBoxPassword = new PasswordTextBox();
	}

	@Override
	public VerticalPanel getContent() {
		if (contactBook.getCurrentSession().isLogged() && content != null) {
			textBoxLogin.setText(contactBook.getCurrentSession().getLogin());
			textBoxPassword.setText(contactBook.getCurrentSession().getPassword());			
		} else {
			textBoxLogin.setText("");
			textBoxPassword.setText("");	
		}
		if (content == null)
			return buildView();
		return content;
	}
	
	public VerticalPanel buildView() {
		
		content = new VerticalPanel();
		
		DecoratorPanel panelLogin = new DecoratorPanel();
		VerticalPanel panelWidgets = new VerticalPanel();
			
		// Note
		final Label labelNote = new Label("Enter both your login and password");
		labelNote.setStyleName("gwt-label-labelnote");
		
		// Form
		final FlexTable table = new FlexTable();
		
		final Label labelLogin = new Label("Login ");
		textBoxLogin.setTitle("Enter your identifiant");
		textBoxLogin.setFocus(true);
		textBoxLogin.setWidth(LOGIN_TEXTBOX_SIZE);
		textBoxLogin.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode())
					submit();
			}
		});
		
		table.setWidget(0, 0, labelLogin);
		table.setWidget(0, 1, textBoxLogin);
		
		final Label labelPassword = new Label("Password ");
		textBoxPassword.setTitle("Enter your account password");
		textBoxPassword.setWidth(PASSWORD_TEXTBOX_SIZE);	
		textBoxPassword.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (KeyCodes.KEY_ENTER == event.getNativeEvent().getKeyCode())
					submit();
			}
		});
		
		table.setWidget(1, 0, labelPassword);
		table.setWidget(1, 1, textBoxPassword);
		
		/* Buttons */
		final Button buttonOk = new Button("Ok");
		buttonOk.addClickHandler(new OkHandler());
		buttonOk.setStyleName("buttonform");
		
		final Button buttonCancel = new Button("Clear");
		buttonCancel.addClickHandler(new CancelHandler());
		buttonCancel.setStyleName("buttonform");
		
		table.setWidget(2, 0, buttonOk);
		table.setWidget(2, 1, buttonCancel);
				
		/* Table properties */
		table.getFlexCellFormatter().setHeight(2, 0, "50px");
		
		table.getFlexCellFormatter().setAlignment(2, 0, ALIGN_CENTER, ALIGN_MIDDLE);
		table.getFlexCellFormatter().setAlignment(2, 1, ALIGN_CENTER, ALIGN_MIDDLE);
		
		table.setCellPadding(6);

		panelWidgets.add(labelNote);
		panelWidgets.add(table);
		
		panelWidgets.setSpacing(5);
		panelLogin.add(panelWidgets);
		
		content.add(panelLogin);
		
		return content;
	}
	
	private class CancelHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			textBoxLogin.setText("");
			textBoxPassword.setText("");
		}
	}
	
	private class OkHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			submit();
		}
	}

	@Override
	public String getTitle() {
		return TITLE;
	}
	
	private void submit() {
		String login = textBoxLogin.getText();
		String password = textBoxPassword.getText();
		contactBook.getCurrentSession().logout();
		if (!login.isEmpty() && !password.isEmpty()) {
			final AsyncCallback<AccountDTO> callbackLogin = new AsyncCallback<AccountDTO>() {

				@Override
				public void onFailure(Throwable caught) {
					new DialogBoxWidget(contactBook, "Error : bad login or password.");
				}

				@Override
				public void onSuccess(AccountDTO result) {
					contactBook.getCurrentSession().logon(result);
					contactBook.setLogOn();
					contactBook.switchView("listOfContacts");						
				}
			};
			contactBook.getAccountService().login(login, password, callbackLogin);
		}
		else {
			new DialogBoxWidget(contactBook, "You must give both your login and your password.").show();
		}
	}
}

